/**
 * 
 */
package com.cmwa.solution.sys.menu.tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import com.cmwa.solution.sys.dictionary.entity.DictionaryVo;
import com.cmwa.solution.utils.cached.DictionaryCached;

/**
 * 下拉框字典选项
 * @author ex-zuotc
 *
 * 2016年11月10日
 */
public class DictOptionsTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	/** 字典根类型 */
	protected String dctRootType;
	/** 数据字典父类型 */
	protected String dctFathType;
	/** 数据字典子类型 */
	protected String dctLeftType;
	/** 默认选中值 */
	protected String initValue;

	public int doStartTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			String resultStr = null;
			List<DictionaryVo> list = null;
			if (StringUtils.isNotBlank(dctFathType) && StringUtils.isNotBlank(dctLeftType)) {
				
				list = DictionaryCached.getDictionaryByType(dctRootType, dctFathType, dctLeftType);
			} else {
				list = DictionaryCached.getDictionaryByRootType(dctRootType);
			}
			StringBuffer stringBuffer = new StringBuffer();
			if (list != null && !list.isEmpty()) {
				for (DictionaryVo vo : list) {
					if (StringUtils.equals(vo.getDctValue(), initValue)) {
						stringBuffer.append("<option value=\"" + vo.getDctValue() + "\" selected=\"selected\">" + vo.getDctName() + "</option>");
					} else {
						if("N".equals(vo.getDctStat())){
							stringBuffer.append("<option value=\"" + vo.getDctValue() + "\"  disabled=\"disabled\">" + vo.getDctName() + "</option>");	
						}else{
							stringBuffer.append("<option value=\"" + vo.getDctValue() + "\">" + vo.getDctName() + "</option>");
						}
					}
				}
			}
			resultStr = stringBuffer.toString();
			
			if (resultStr != null) {
				out.print(resultStr);
			}
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	public String getDctRootType() {
		return dctRootType;
	}

	public void setDctRootType(String dctRootType) {
		this.dctRootType = dctRootType;
	}

	public String getInitValue() {
		return initValue;
	}

	public void setInitValue(String initValue) {
		this.initValue = initValue;
	}

	public String getDctFathType() {
		return dctFathType;
	}

	public void setDctFathType(String dctFathType) {
		this.dctFathType = dctFathType;
	}

	public String getDctLeftType() {
		return dctLeftType;
	}

	public void setDctLeftType(String dctLeftType) {
		this.dctLeftType = dctLeftType;
	}
	
}
